/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trabalho;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * @author Maria
 */
public class Depreciacao extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;

//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form form;
    private TextField vlTextField;
    private TextField hUsoTextField;
    private TextField qtHTextField;
    private ChoiceGroup bensChoiceGroup;
    private Spacer spacer;
    private StringItem resultadoStringItem;
    private Command exitCommand;
    private Command calcularCommand;
    private Command backCommand;
//</editor-fold>//GEN-END:|fields|0|
    /**
     * The Depreciacao constructor.
     */
    public Depreciacao() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display;
     * if <code>null</code>, then <code>nextDisplayable</code> is set
     * immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|18-preAction
            if (command == calcularCommand) {//GEN-END:|7-commandAction|1|18-preAction
                try{
                    double r=1;
               double vl = Double.parseDouble(vlTextField.getString());//recebe o valor da variavel
               vlTextField.setString("");//limpar os campos
               double hTrabalho = Double.parseDouble(qtHTextField.getString());
               qtHTextField.setString("");
               double hUso = Double.parseDouble(hUsoTextField.getString());
               hUsoTextField.setString("");
               if(bensChoiceGroup.isSelected(0)){//condições para cada escolha
               r=(((vl*0.2)/12)/hTrabalho)*hUso;//formula para o calculo
              
              
               }else if(bensChoiceGroup.isSelected(1)){
                  r=(((vl*0.1)/12)/hTrabalho)*hUso; 

               }else if(bensChoiceGroup.isSelected(2)){
                   r=(((vl*0.2)/12)/hTrabalho)*hUso;

               }else if(bensChoiceGroup.isSelected(3)){
                   r=(((vl*0.1)/12)/hTrabalho)*hUso;

               }else if(bensChoiceGroup.isSelected(4)){
                   r=(((vl*0.04)/12)/hTrabalho)*hUso;

               }         
             
               resultadoStringItem.setText("$ "+r);//apresentar a resposta

                }catch(ArithmeticException e){//alerta caso aja uma excessão
                   Alert erro = new Alert("ERRO");
                    erro.setString("Digite novamente");
                    erro.setTimeout(3000);
                    getDisplay().setCurrent(erro);


                }
//GEN-LINE:|7-commandAction|2|18-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|3|16-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|4|16-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
        }//GEN-END:|7-commandAction|5|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|6|
//</editor-fold>//GEN-END:|7-commandAction|6|


//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initialized instance of form component.
     *
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form("Deprecia\u00E7\u00E3o", new Item[]{getVlTextField(), getQtHTextField(), getHUsoTextField(), getBensChoiceGroup(), getSpacer(), getResultadoStringItem()});//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getExitCommand());
            form.addCommand(getCalcularCommand());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: vlTextField ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initialized instance of vlTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getVlTextField() {
        if (vlTextField == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            vlTextField = new TextField("Valor base do bem:", "", 32, TextField.NUMERIC);//GEN-LINE:|21-getter|1|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return vlTextField;
    }
//</editor-fold>//GEN-END:|21-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: qtHTextField ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initialized instance of qtHTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getQtHTextField() {
        if (qtHTextField == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            qtHTextField = new TextField("Qtd. horas trabalhadas(200h ou 220h):", "", 32, TextField.NUMERIC);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return qtHTextField;
    }
//</editor-fold>//GEN-END:|22-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: hUsoTextField ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initialized instance of hUsoTextField component.
     *
     * @return the initialized component instance
     */
    public TextField getHUsoTextField() {
        if (hUsoTextField == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            hUsoTextField = new TextField("Qtd.horas de uso do bem:", "", 32, TextField.NUMERIC);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return hUsoTextField;
    }
//</editor-fold>//GEN-END:|23-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: bensChoiceGroup ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initialized instance of bensChoiceGroup component.
     *
     * @return the initialized component instance
     */
    public ChoiceGroup getBensChoiceGroup() {
        if (bensChoiceGroup == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            bensChoiceGroup = new ChoiceGroup("Op\u00E7\u00E3o de Bens:", Choice.EXCLUSIVE);//GEN-BEGIN:|24-getter|1|24-postInit
            bensChoiceGroup.append("Veiculo (5 anos).", null);
            bensChoiceGroup.append("Maquinas/Equipamentos (10 anos).", null);
            bensChoiceGroup.append("Computador (5 anos).", null);
            bensChoiceGroup.append("Moveis (10 anos).", null);
            bensChoiceGroup.append("Edifica\u00E7\u00F5es (25 anos).", null);
            bensChoiceGroup.setSelectedFlags(new boolean[]{false, false, false, false, false});//GEN-END:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return bensChoiceGroup;
    }
//</editor-fold>//GEN-END:|24-getter|2|





//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|15-getter|0|15-preInit
    /**
     * Returns an initialized instance of exitCommand component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|15-getter|0|15-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|15-getter|1|15-postInit
            // write post-init user code here
        }//GEN-BEGIN:|15-getter|2|
        return exitCommand;
    }
//</editor-fold>//GEN-END:|15-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: calcularCommand ">//GEN-BEGIN:|17-getter|0|17-preInit
    /**
     * Returns an initialized instance of calcularCommand component.
     *
     * @return the initialized component instance
     */
    public Command getCalcularCommand() {
        if (calcularCommand == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
            calcularCommand = new Command("Calcular", Command.ITEM, 0);//GEN-LINE:|17-getter|1|17-postInit
            // write post-init user code here
        }//GEN-BEGIN:|17-getter|2|
        return calcularCommand;
    }
//</editor-fold>//GEN-END:|17-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initialized instance of backCommand component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|31-getter|1|31-postInit
            // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|31-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initialized instance of spacer component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 1);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return spacer;
    }
//</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: resultadoStringItem ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initialized instance of resultadoStringItem component.
     *
     * @return the initialized component instance
     */
    public StringItem getResultadoStringItem() {
        if (resultadoStringItem == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            resultadoStringItem = new StringItem("Resultado", "");//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return resultadoStringItem;
    }
//</editor-fold>//GEN-END:|40-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
